#!/usr/bin/env bash

set -exu
__DIR__=$(
  cd "$(dirname "$0")"
  pwd
)
__PROJECT__=$(
  cd "${__DIR__}/../"
  pwd
)

cd ${__PROJECT__}

while [ $# -gt 0 ]; do
  case "$1" in
  --proxy)
    export HTTP_PROXY="$2"
    export HTTPS_PROXY="$2"
    NO_PROXY="127.0.0.0/8,10.0.0.0/8,100.64.0.0/10,172.16.0.0/12,192.168.0.0/16"
    NO_PROXY="${NO_PROXY},::1/128,fe80::/10,fd00::/8,ff00::/8"
    NO_PROXY="${NO_PROXY},localhost"
    NO_PROXY="${NO_PROXY},.aliyuncs.com,.aliyun.com,.tencent.com"
    NO_PROXY="${NO_PROXY},.myqcloud.com,.swoole.com"
    export NO_PROXY="${NO_PROXY},.tsinghua.edu.cn,.ustc.edu.cn,.npmmirror.com"
    ;;
  esac
  shift $(($# > 0 ? 1 : 0))
done

mkdir -p ${__PROJECT__}/var/
cd ${__PROJECT__}/var/

curl -fSLo setup-nginx-runtime.sh https://github.com/jingjingxyk/swoole-cli/blob/new_dev/setup-nginx-runtime.sh?raw=true
curl -fSLo setup-php-cli-runtime.sh https://github.com/jingjingxyk/swoole-cli/blob/new_dev/setup-php-cli-runtime.sh?raw=true
curl -fSLo setup-php-fpm-7.4-runtime.sh https://github.com/jingjingxyk/swoole-cli/blob/new_dev/setup-php-fpm-7.4-runtime.sh?raw=true
curl -fSLo setup-php-fpm-runtime.sh https://github.com/jingjingxyk/swoole-cli/blob/new_dev/setup-php-fpm-runtime.sh?raw=true
curl -fSLo setup-swoole-cli-pre-runtime.sh https://github.com/jingjingxyk/swoole-cli/blob/new_dev/setup-swoole-cli-pre-runtime.sh?raw=true
curl -fSLo setup-swoole-cli-runtime.sh https://github.com/jingjingxyk/swoole-cli/blob/new_dev/setup-swoole-cli-runtime.sh?raw=true
curl -fSLo setup-swow-cli-runtime.sh https://github.com/jingjingxyk/swoole-cli/blob/new_dev/setup-swow-cli-runtime.sh?raw=true

mv -f setup-nginx-runtime.sh ${__PROJECT__}
mv -f setup-php-cli-runtime.sh ${__PROJECT__}
mv -f setup-php-fpm-7.4-runtime.sh ${__PROJECT__}
mv -f setup-php-fpm-runtime.sh ${__PROJECT__}
mv -f setup-swoole-cli-pre-runtime.sh ${__PROJECT__}
mv -f setup-swoole-cli-runtime.sh ${__PROJECT__}
mv -f setup-swow-cli-runtime.sh ${__PROJECT__}

cd ${__PROJECT__}/